package com.upc.toss.fraction;

import com.upc.toss.ResourceTable;
import com.upc.toss.slice.MainAbilitySlice;
import com.upc.toss.slice.SecondAbilitySlice;
import com.upc.toss.util.Get;
import com.upc.toss.util.Update;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.app.dispatcher.TaskDispatcher;
import ohos.app.dispatcher.task.Revocable;
import ohos.app.dispatcher.task.TaskPriority;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

public class SecondFraction extends Fraction {
    private static final String TAG="SecondFraction";
    private int MY_MODULE=0x00202;
    private   final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, MY_MODULE, TAG); //

    boolean isLightOn,isMotorOn=true;

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container,
                                            Intent intent) {

        Component component = scatter.parse(ResourceTable.Layout_fraction_second, container, false);

        return component;
    }
    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        TaskDispatcher globalTaskDispatcher = getGlobalTaskDispatcher(TaskPriority.DEFAULT);

        Text textSTemperature=(Text) getFractionAbility().findComponentById(ResourceTable.Id_text_tem_warning);
        Text textSHumanity=(Text) getFractionAbility().findComponentById(ResourceTable.Id_text_hum_warning) ;
        Text textSLuminance=(Text) getFractionAbility().findComponentById(ResourceTable.Id_text_lum_warning) ;

        textSTemperature.setText(Get.settingTemperature);
        textSHumanity.setText(Get.settingHumidity);
        textSLuminance.setText(Get.settingLuminance);

        TextField textSetTep =(TextField) getFractionAbility().findComponentById(ResourceTable.Id_text_tem_setting);
        TextField textSetHum=(TextField) getFractionAbility().findComponentById(ResourceTable.Id_text_hum_setting);
        TextField textSetLum=(TextField) getFractionAbility().findComponentById(ResourceTable.Id_text_lux_setting);


        Button buttonTmp =(Button) getFractionAbility().findComponentById(ResourceTable.Id_button_tem_setting);
        Button buttonHum=(Button) getFractionAbility().findComponentById(ResourceTable.Id_button_hum_setting);
        Button buttonLum=(Button) getFractionAbility().findComponentById(ResourceTable.Id_button_lux_setting);

        buttonTmp.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSTemperature.setText(textSetTep.getText());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Temperature","Setting_Temperature",textSetTep.getText());
                    }
                }).start();
            }
        });
        buttonHum.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSHumanity.setText(textSetHum.getText());
                new Thread(new Runnable(){
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Humidity","Setting_Humidity",textSetHum.getText());
                    }
                });
            }
        });
        buttonLum.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                textSLuminance.setText(textSetLum.getText());
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        Update.postCommandValue("Set_Luminance","Setting_Luminance",textSetLum.getText());
                    }
                });
            }
        });

        Switch btnSwitchLight = (Switch) getFractionAbility().findComponentById(ResourceTable.Id_btn_switch_light);
        Switch btnSwitchMotor = (Switch) getFractionAbility().findComponentById(ResourceTable.Id_btn_switch_motor);



        btnSwitchMotor.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            // 回调处理Switch状态改变事件
            @Override
            public void onCheckedChanged(AbsButton button, boolean isChecked) {
                btnSwitchMotor.setClickable(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(isMotorOn){
                            Update.postCommand("Control_Motor","Motor","OFF");
                            isMotorOn=!isMotorOn;
                            try {
                                Thread.sleep(3500);
                                btnSwitchMotor.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            Update.postCommand("Control_Motor","Motor","ON");
                            isMotorOn=!isMotorOn;
                            try {
                                Thread.sleep(3500);
                                btnSwitchMotor.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });
        btnSwitchLight.setCheckedStateChangedListener(new AbsButton.CheckedStateChangedListener() {
            // 回调处理Switch状态改变事件
            @Override
            public void onCheckedChanged(AbsButton button, boolean isChecked) {
                btnSwitchLight.setClickable(false);
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        if(isLightOn){
                            Update.postCommand("Control_Motor","Motor","OFF");
                            isLightOn=!isLightOn;
                            try {
                                Thread.sleep(3500);
                                btnSwitchLight.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }else {
                            Update.postCommand("Control_Motor","Motor","ON");
                            isLightOn=!isLightOn;
                            try {
                                Thread.sleep(3500);
                                btnSwitchLight.setClickable(true);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                    }
                }).start();
            }
        });


    }
    @Override
    protected void onActive() {
        super.onActive();

    }
    @Override
    protected void onInactive() {
        super.onInactive();

    }
    @Override
    protected void onBackground() {
        super.onBackground();

    }
    @Override
    protected void onForeground(Intent intent) {
        super.onForeground(intent);

    }
    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    protected void onComponentDetach() {
        super.onComponentDetach();

    }
}

